import cookie from '../../assets/js/cookie.js'
import moudleConfig from "../../assets/js/moudleName.js"
import md5 from 'js-md5';
import { parseQueryString } from "../../assets/js/util.js";
export default {
    data() {
        return {
            // 通用的SRC
            SRC: '',
            username: '',
            userPd: '',
            pw: '',
            pwPd: '',
            ispointer: true,
            rememberValue: '',
            checkRember: false,
            //  选择OA 或者 BI账号
            account: true,
            // 注销展示
            switchAccountShow: false
        }
    },
    methods: {
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        userFocus() {},
        userBlur() {
            var value = this.username;
            if (value === null) {
                return false;
            } else if (value.length === 0) {
                this.userPd = "*账号不能为空！"
                this.ispointer = true;
                return false;
            } else if (value.length > 20) {
                this.username = value.substring(0, 20);
            } else if (value.length > 0 && value.length < 3) {
                this.userPd = "*ID名最少为3位字符，请重新输入！";
                this.ispointer = true;
                return false;
            } else if (value.length > 2 && value.length < 21) {
                this.userReg();
                this.pwPd = " ";
            }
            return true;
        },
        pwFocus() {},
        pwBlur() {
            var value = this.pw;
            // this.$refs.searchInput3.focus();
            if (value.length == 0) {

                this.pwPd = "*密码不能为空！"
                this.ispointer = true;
                return false;
            } else if (value.length > 0 && value.length < 6) {
                this.pwPd = "*密码最少为6位字符，请重新输入！"
                this.ispointer = true;
                return false;
            } else if (value.length > 5) {
                this.pwReg();
            }
            return true;
        },
        pwMouse() {
            if (this.pwBlur() && this.userBlur()) {
                this.$refs.searchInput3.focus();
            } else if (!this.userBlur()) {
                this.$refs.searchInput1.focus();
                this.pwPd = "";
            } else if (!this.pwBlur()) {
                this.$refs.searchInput2.focus();
            }
        },
        userReg() {
            const userID = /^[\u4e00-\u9fa5A-Za-z0-9-_]{3,20}$/;
            let value = this.username;
            this.usernameCp = value;
            let result = userID.test(value);
            if (result == false) {
                this.userPd = "*ID名只能为中英文，数字，下划线，减号！";
                this.ispointer = true;
                this.$refs.searchInput1.focus();
            } else {
                this.userPd = " ";
            }
            return result;
        },
        pwReg() {
            const pwGz = /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{6,}|(?:(?=.*[A-Z])(?=.*[a-z])|(?=.*[A-Z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])|(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[0-9])(?=.*[^A-Za-z0-9])|).{6,}$/;
            let value = this.pw;
            this.pwCp = value;
            let result = pwGz.test(value);
            if (result == false) {
                this.pwPd = "*密码格式错误！";
                this.ispointer = true;
                this.$refs.searchInput2.focus();
            } else {
                this.pwPd = " ";
                this.ispointer = false;
            }
            return result;
        },
        bigFou() {
            if (this.pwBlur() && this.userBlur()) {
                // this.$refs.searchInput3.focus();
            } else if (!this.userBlur()) {
                this.userBlur();
                this.$refs.searchInput1.focus();
                this.pwPd = "";
            } else if (!this.pwBlur()) {
                this.pwBlur();
                this.$refs.searchInput2.focus();
            } else {
                this.$refs.searchInput1.focus();
            }
        },
        userCli(event) {
            event.cancelBubble = true;
        },
        pwCli(event) {
            event.cancelBubble = true;
        },
        loginIn() {
            if (this.userReg() && this.pwReg()) {
                if (this.pw.length < 18) {
                    this.pw = md5(this.pw);
                }
                this.getData({ u: this.username, p: this.pw });
            } else {
                this.ispointer = true;
            }
        },
        showPw() {
            if (this.userBlur()) {
                this.$refs.searchInput2.focus();
            } else {
                this.userBlur();
                this.$refs.searchInput1.focus();
            }
        },
        // 回车登录
        show() {
            if (this.pwBlur() && this.userBlur()) {
                if (this.pw.length > 18) {
                    this.getData({ u: this.username, p: this.pw });
                } else {
                    this.getData({ u: this.username, p: this.pw, ys: 'ys' });
                }
            } else {
                this.pwBlur();
            }
        },
        // errcode
        errCode(num) {
            var code = {
                '1': '请求异常，请检查IP，用户名、密码！',
                '2': '用户名或密码不能为空！请填写！',
                '3': '用户名或密码错误,请注意大小写！',
                '4': '无此用户，请找祖龙BI团队！'
            }
            return code[num]
        },
        // 保存用户名和密码到客户端
        save(user, pass) {
            if (window.localStorage) {
                localStorage.setItem("ZULONGBIUSER", user);
                localStorage.setItem('ZULONGBIPASSWORD', pass);
            } else {
                cookie.set("ZULONGBIUSER", user, 2000);
                cookie.set("ZULONGBIPASSWORD", pass, 2000);
            }
        },
        /*清除用户名和密码到客户端*/
        clearCache() {
            if (window.localStorage) {
                localStorage.removeItem('ZULONGBIPASSWORD');
            } else {
                cookie.del('ZULONGBIPASSWORD');
            }
        },
        // 获取到用户名和密码
        getCache() {
            if (window.localStorage) {
                localStorage.getItem("ZULONGBIUSER");
                if (localStorage.getItem("ZULONGBIPASSWORD")) {
                    this.username = localStorage.getItem("ZULONGBIUSER");
                    this.pw = localStorage.getItem("ZULONGBIPASSWORD");
                    this.rememberValue = true;
                } else {
                    this.rememberValue = false;
                    this.username = localStorage.getItem("ZULONGBIUSER");
                }
            }
        },
        // 记住密码
        rememberPassword(value) {
            this.rememberValue = value;
            if (!this.rememberValue) {
                this.pw = '';
                this.clearCache();
            }
        },
        // 请求后台数验证
        getData(msg) {
            // 数组排序
            var arraySort = function(d) {
                var numberSort = function(a, b) {
                    return a - b
                }
                for (var i in d) {
                    for (var j in d[i]) {
                        d[i][j] = d[i][j].sort(numberSort);
                    }
                }
                return d
            };
            // arr.sort(arraySort)
            this.$http.jsonp(`${this.SRC}user/login.do`, {
                params: msg
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                if (data.res === '0') {
                    data.data = arraySort(data.data);
                    localStorage.setItem("ZULONGBIUSERID", data.i);
                    this.$store.commit('REMEMBERUSER', { zulongBiUser: data.u, zulongBiPassword: data.p });
                    localStorage.setItem("ZULONGBIUSER", data.u);
                    localStorage.setItem('ZULONGBIISADMIN', data.isAdmin);
                    if (!localStorage.getItem("ZULONGBIPASSWORD")) {
                        if (this.rememberValue === true) {
                            this.save(data.u, data.p)
                        } else {
                            this.save(data.u, '')
                        }
                    }
                    this.$store.commit('NAVLIST', data.data);
                    // 得到首个路由
                    for (var i in data.data) {
                        for (var j in data.data[i]) {
                            for (var n in moudleConfig[j].pages) {
                                this.$router.push({ path: moudleConfig[j].pages[n].index });
                                break;
                            }
                            break;
                        }
                        break;
                    }
                    // this.$router.push({ path: '/main' });
                } else {
                    this.errorFun(this.errCode(data.res));
                }
            })
        },
        // sso 登录
        ssoLogin(data) {
            if (typeof(data) == "object") {
                return;
            }
            if (data.indexOf("&sign") < 0) {
                return;
            }
            var _uuid = cookie.get("uuid");
            console.log(_uuid)
            if (_uuid == "") {
                return;
            }
            var obj = parseQueryString(data);
            var newObj = { callback: obj.callback, sign: obj.sign, timestamp: obj.timestamp, uuid: _uuid, appid: "zlbiweb_OA" };
            localStorage.setItem("sign", obj.sign);
            localStorage.setItem("timestamp", obj.timestamp);
            localStorage.setItem("uuid", obj.uuid);
            // 数组排序
            var arraySort = function(d) {
                var numberSort = function(a, b) {
                    return a - b;
                };
                for (var i in d) {
                    for (var j in d[i]) {
                        d[i][j] = d[i][j].sort(numberSort);
                    }
                }
                return d;
            };
            this.$http
                .jsonp(
                    "http://bi.zulong.com/mid/user/ssologin.do", {
                        params: newObj
                    }
                )
                .then(function(res) {
                    var data = JSON.parse(JSON.parse(res.body));
                    if (data.res === "0") {
                        data.data = arraySort(data.data);
                        localStorage.setItem("ZULONGBIUSERID", data.i);
                        this.$store.commit(
                            "REMEMBERUSER", {
                                zulongBiUser: data.u,
                                zulongBiPassword: data.p
                            }
                        );
                        localStorage.setItem("ZULONGBIUSER", data.u);
                        localStorage.setItem("ZULONGBIISADMIN", data.isAdmin);
                        if (!localStorage.getItem("ZULONGBIPASSWORD")) {
                            if (this.rememberValue === true) {
                                this.save(data.u, data.p);
                            } else {
                                this.save(data.u, "");
                            }
                        }
                        this.$store.commit("NAVLIST", data.data);
                        // 得到首个路由
                        for (var i in data.data) {
                            for (var j in data.data[i]) {
                                for (var n in moudleConfig[j].pages) {
                                    console.log(moudleConfig[j].pages[n].index);
                                    this.$router.push({
                                        path: moudleConfig[j].pages[n]
                                            .index
                                    });
                                    break;
                                }
                                break;
                            }
                            break;
                        }
                        // this.$router.push({ path: '/main' });
                    } else {
                        if (data.res == 1 || data.res == 2 || data.res == 3) {
                            this.errorFun("登录失败！");
                        } else if (data.res == 4 || data.res == 5) {
                            this.errorFun("该用户没有该系统权限！请申请开通权限！");
                            this.switchAccountShow = true;
                        }
                        cookie.del("uuid");
                        document.cookie = "uuid=; domain=.zulong.com;";
                        document.getElementById("login_iframe").src = "http://sso-test.zulong.com/acs_sso/view/5?appid=zlbiweb_OA&callback=http://bi.zulong.com";
                    }
                });
        },
        // 选择oa还是bi用户
        chooseId(num) {
            this.account = num;
        }
    },
    mounted() {
        var that = this;
        this.SRC = this.$store.getters.src;
        this.getCache();
        window.addEventListener("message", function(e) {
            that.ssoLogin(e.data);
        }, false);
        // console.log(this.$store.getters.setuser)
    }
}